﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace EFCodeFirstLearn
{
    [Table("My_Student")]
    class Student
    {
        [Key]
        public int Id { set; get; }
        [MaxLength(50)]
        public string Name { set; get; }
        [MaxLength(10)]
        public string Sex { set; get; }
        public int Age { set; get; }
        [MaxLength(500)]
        public string Address { set; get; }
        [MaxLength(1000)]
        public string Remark { set; get; }
    }

    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    class MyContext : DbContext
    {
        public MyContext() : base("MySqlConString") { }
        public DbSet<Student> StudentSet { set; get; }
    }

    class APP
    {
        public APP()
        {
            while (true)
            {
                Console.Write("姓名：");
                var Name = Console.ReadLine();
                Console.Write("性别：");
                var Sex = Console.ReadLine();
                Console.Write("年龄：");
                var Age = int.Parse(Console.ReadLine());
                Console.Write("地址：");
                var Address = Console.ReadLine();

                using (MyContext DB = new MyContext())
                {
                    Student St = new Student();
                    St.Name = Name;
                    St.Sex = Sex;
                    St.Age = Age;
                    St.Address = Address;

                    DB.StudentSet.Add(St);
                    DB.SaveChanges();

                    Console.WriteLine("保存成功，编号为：" + St.Id);
                    Console.WriteLine("");
                }
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            APP App = new APP();
        }
    }


    /*
     如果需要更新数据库
    NuGet包管理器控制台
        Enable-Migrations
        AutomaticMigrationsEnabled = true;
        Update-Database
     */
}
